package com.fcl.oschina.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.fcl.mylibrary.banner.DataType;
import com.fcl.mylibrary.banner.ImageSrc;
import com.fcl.mylibrary.banner.MyPagerAdapter;
import com.fcl.mylibrary.banner.MyPagerConfig;
import com.fcl.mylibrary.banner.MyPagerLayout;
import com.fcl.mylibrary.recyclerviewhelper.BaseAdapter;
import com.fcl.mylibrary.reflect.ViewInject;
import com.fcl.mylibrary.util.FLog;
import com.fcl.oschina.R;
import com.fcl.oschina.adapter.NewsAdapter;
import com.fcl.oschina.entity.NewItem;
import com.fcl.oschina.model.NewsModel;
import com.fcl.oschina.ui.activity.BlogDetailActivity;
import com.fcl.oschina.ui.activity.NewsDetailActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class NewsFragment extends BaseRecyclerFragment<NewItem> implements NewsView {

    private String TAG = "NewsFragment";
    private NewsModel mNewsModel;
    private NewsAdapter mNewsAdapter;

    /**
     * banner相关
     */
    private MyPagerConfig<String> bannerConfig;
    protected MyPagerAdapter<String> bannerAdapter;
    protected List<String> bannerDatas = new ArrayList<>();

    private String[] textDatas = {"测试", "再测试", "今天放假", "哈哈哈"};

    @ViewInject(R.id.banner)
    MyPagerLayout mPageLayout;

    @Override
    protected void initData() {
        super.initData();
        mNewsModel = new NewsModel(this);
        mNewsModel.setNextPage("");
        mNewsModel.getNewsList();
        mNewsAdapter = (NewsAdapter) mAdapter;
        mNewsAdapter.setItemClickListener(new BaseAdapter.MyClickListener() {
            @Override
            public void onClick(int pos) {
//                ToastUtil.showDefineToast("点击位置:"+pos);
                NewItem newItem = mDatas.get(pos);
                jumpActivity(newItem.getType(), newItem.getId());
//                startActivity(new Intent(getActivity(), NewsDetailActivity.class));
            }
        });
        mNewsModel.getBanner(1);
    }



    @Override
    protected void init() {

        initBanner();
        super.init();
    }

    private void initBanner() {
        MyPagerConfig.Builder<String> builder = new MyPagerConfig.Builder<>();
        builder.setDataType(DataType.IMAGE); //设置数据类型为图片
        builder.setImageSrcType(ImageSrc.NET_URL); //设置图片来源为网络
        bannerConfig = builder.create();
        bannerAdapter = new MyPagerAdapter<String>(getContext(), bannerDatas);
        mPageLayout.setTextDatas(Arrays.asList(textDatas));
        mPageLayout.setPointPos(MyPagerLayout.PointPos.RIGHT_BOTTOM);
        mPageLayout.setTextPos(MyPagerLayout.PointPos.LEFT_BOTTOM);
        bannerAdapter.initConfig(bannerConfig);
        mPageLayout.setPagerAdapter(bannerAdapter);
        mPageLayout.setPoints(0);
        mPageLayout.setScrollDuration(3000);
        mPageLayout.startScroll();
    }


    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new NewsAdapter(getContext(), R.layout.item_news,  mDatas);
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        mSceneLayout.setLoading();
        mNewsModel.setNextPage("");
        mNewsModel.getNewsList();
    }

    @Override
    public void setBanner(List<String> data) {
        this.bannerDatas = data;
        mPageLayout.setImageNum(bannerDatas.size());
        bannerAdapter.setmShowDatas(bannerDatas);
        mPageLayout.setPoints(0);
    }

    @Override
    public void setData(final List<NewItem> data, final boolean add) {
        FLog.error(TAG, " thread" + Thread.currentThread().getId());
        if (add) {
            mDatas.addAll(data);
        } else {
            mDatas = data;
        }
        mNewsAdapter.setDatas(mDatas);
        mStickyLayout.finishRefresh();
        mSceneLayout.setNormal();
//        getActivity().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
    }

    @Override
    public void noNet() {

    }

    @Override
    protected void loadMore(int curPage) {
        super.loadMore(curPage);
        mNewsModel.getNewsList();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news;
    }
}
